package api

import (
	"api-getway/basic/config"
	__ "api-getway/basic/proto"
	"api-getway/handler/request"
	"api-getway/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
	"path/filepath"
)

func Upload(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		return
	}

	upload, err := pkg.QiNiuUpload(file)
	if err != nil {
		return
	}

	if file.Size > 1*1024*1024*1024 {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "文件大小不能大于1GB",
		})
		return
	}

	ext := filepath.Ext(file.Filename)
	if ext != ".mp4" {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "文件格式必须为 .mp4",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "文件上传成功",
		"url":  upload,
	})
}

func AddVideo(c *gin.Context) {
	var err error
	var req request.AddVideoReq

	if err = c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
		return
	}

	login, err := config.VideoClient.AddVideo(c, &__.AddVideoReq{
		Title:    req.Title,
		Desc:     req.Briefly,
		TypesId:  req.TypesId,
		Ads:      req.Ads,
		Channel:  req.Channel,
		CoverUrl: req.CoverUrl,
		PlayUrl:  req.PlayUrl,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  "视频信息录入失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":   200,
		"msg":    "视频信息录入成功",
		"userId": login.Id,
	})
}

func CheckVideo(c *gin.Context) {
	var err error
	var req request.CheckVideoReq

	if err = c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
		return
	}

	check, err := config.VideoClient.CheckVideo(c, &__.CheckVideoReq{
		VideoId:     req.VideoId,
		Status:      req.CheckStatus,
		CheckReason: req.CheckReason,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  "视频信息录入失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"msg":     "驳回审核申请",
		"videoId": check.VideoId,
	})
}

func VideoList(c *gin.Context) {
	var err error
	var req request.VideoListReq

	if err = c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
		return
	}

	list, err := config.VideoClient.VideoList(c, &__.VideoListReq{
		Page:     req.Page,
		PageSize: req.PageSize,
		TypesId:  req.Types,
		Channel:  req.Channel,
		Ads:      req.Ads,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  "视频列表展示失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "视频列表展示成功",
		"data": list,
	})
}

func VideoInfo(c *gin.Context) {
	var err error
	var req request.VideoInfoReq

	if err = c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
		return
	}

	info, err := config.VideoClient.VideoInfo(c, &__.VideoInfoReq{
		VideoId: req.VideoId,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "视频详情展示成功",
		"data": info,
	})
}

func EpisodeList(c *gin.Context) {
	var err error

	list, err := config.VideoClient.EpisodeList(c, &__.EpisodeListReq{})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  "分级列表展示失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "分级列表展示成功",
		"data": list,
	})
}

func CreatePlayRecord(c *gin.Context) {
	var err error
	var req request.CreatePlayRecordReq

	if err = c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
		return
	}

	add, err := config.VideoClient.CreatePlayRecord(c, &__.CreatePlayRecordReq{
		VideoId: req.VideoId,
		Episode: req.EpisodeId,
		UserId:  req.UserId,
		Seconds: req.Seconds,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "用户观看历史记录成功",
		"data": add,
	})
}

func PlayRecordList(c *gin.Context) {
	var err error
	var req request.PlayRecordListReq

	if err = c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
		return
	}

	list, err := config.VideoClient.PlayRecordList(c, &__.PlayRecordListReq{
		UserId: req.UserId,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "用户观看历史记录列表展示成功",
		"data": list,
	})
}

func PlayRecordDel(c *gin.Context) {
	var err error
	var req request.PlayRecordDelReq

	if err = c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
		return
	}

	_, err = config.VideoClient.PlayRecordDel(c, &__.PlayRecordDelReq{
		UserId: req.UserId,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "用户观看历史记录清空成功",
	})
}

func TotalHot(c *gin.Context) {
	var err error
	var req request.TotalHotReq

	if err = c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
		return
	}

	_, err = config.VideoClient.TotalHot(c, &__.TotalHotReq{
		VideoId: req.VideoId,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "热度统计完成",
	})
}

func SyncToEs(c *gin.Context) {
	var err error
	var req request.SyncToEsReq

	if err = c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
		return
	}

	hot, err := config.VideoClient.SyncToEs(c, &__.SyncToEsReq{
		VideoId: req.VideoId,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "数据同步成功！",
		"hot":  hot,
	})
}

func THeHot(c *gin.Context) {
	var err error

	list, err := config.VideoClient.TheHot(c, &__.TheHotReq{})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  "热度排行榜展示失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "热度排行榜展示成功",
		"data": list,
	})
}
